﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "AddStructureDynamicPannelUtil.h"
#include "GcmpActionBase.h"
#include "GbmpControlIds.h"
#include "IGalleryDefinition.h"
#include "ICommandButtonDefinition.h"
#include "IRibbonGroup.h"
#include "StructureCmdIds.h"
#include "IApplication.h"
#include "IMainWindow.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

namespace
{
    // 基于新UI框架实现的Gallery
    OwnerPtr<IGalleryDefinition> CreateDrawGalleryDefinition(IAction* pAction, const std::vector<std::wstring>& cmdStrings,
        const std::vector<std::wstring>& names, const std::vector<std::wstring>& icons)
    {
        OwnerPtr<IGalleryDefinition> opGalleryDef = IGalleryDefinition::Create(L"gmDrawGallery", GBMP_TR(L"绘制方式"));
        for (size_t i = 0; i < cmdStrings.size(); ++i)
        {
            opGalleryDef->AddItem(cmdStrings[i], names[i], names[i], icons[i], cmdStrings[i]);
        }
        opGalleryDef->SetCurrentGalleryItem(pAction->GetCommandId());

        return opGalleryDef;
    }
}

void AddStructureDynamicPannelUtil::SetupWallCreationContext(IAction* pAction, const std::wstring& pageName, bool isVariableSection)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pAction!=nullptr,L"无效Action",L"GDMPLab",L"2024-03-30");

    std::vector<std::wstring> cmdStrings;
    std::vector<std::wstring> names;
    std::vector<std::wstring> icons;

    cmdStrings.push_back(isVariableSection ? ID_CMD_DRAW_VARIABLE_SECTION_STRUCTURE_STRAIGHT_WALL : ID_CMD_DRAW_STRUCTURE_STRAIGHT_WALL);
    names.push_back(GBMP_TR(L"直墙"));
    icons.push_back(L":/images/sketchline/直线16px.png");

    cmdStrings.push_back(isVariableSection ? ID_CMD_CREATE_RECT_STRUCTURE_VARIABLE_SECTION_WALL : ID_CMD_CREATE_RECT_STRUCTURE_WALL);
    names.push_back(GBMP_TR(L"矩形墙"));
    icons.push_back(L":/images/sketchline/矩形16px.png");

    if (!isVariableSection) // 变截面墙只能是直线墙
    {
        cmdStrings.push_back(ID_CMD_DRAW_STRUCTURE_ARC_WALL);
        names.push_back(GBMP_TR(L"弧墙"));
        icons.push_back(L":/images/sketchline/三点圆弧16px.png");
    }

    cmdStrings.push_back(isVariableSection ? ID_CMD_CREATE_STRUCTURE_VARIABLE_SECTION_WALL_BY_PICK : ID_CMD_CREATE_STRUCTURE_WALL_BY_PICK);
    names.push_back(GBMP_TR(L"拾取线"));
    icons.push_back(L":/images/sketchline/选择线16px.png");
    
    {
        std::vector<OwnerPtr<IRibbonGroup>> ribbonGroups;
        OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(L"gmDrawWallDynamicGroup", GBMP_TR(L"绘制"));
        {
            OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(pAction, cmdStrings, names, icons);
            opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
        }
        ribbonGroups.push_back(TransferOwnership(opDrawGroup));

        GcmpActionBase* pActionBase = dynamic_cast<GcmpActionBase*>(pAction);
        if (pActionBase)
        {
            pActionBase->AddDynamicGroup(pageName, TransferOwnership(ribbonGroups), GBMP_TR(L"创建墙"));
        }
    }
}

void AddStructureDynamicPannelUtil::SetupWallHoleCreationContext(IAction* pAction, const std::wstring& pageName)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pAction != nullptr, L"无效Action",L"GDMPLab",L"2024-03-30");

    {
        std::vector<OwnerPtr<IRibbonGroup>> ribbonGroups;
        OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(L"gmPlaceWallHoleGroup", GBMP_TR(L"放置"));
        {
            OwnerPtr<ICommandButtonDefinition> opBtnDef = ICommandButtonDefinition::Create(ID_CMD_DRAW_RECT_STRUCTURE_WALL_HOLE, GBMP_TR(L"一点放置"), GBMP_TR(L"一点放置"),
                ID_CMD_DRAW_RECT_STRUCTURE_WALL_HOLE, L":/images/wall_opening.png", ButtonStyle::TextUnderIcon);
            opDrawGroup->AddButton(TransferOwnership(opBtnDef));
        }
        ribbonGroups.push_back(TransferOwnership(opDrawGroup));

        GcmpActionBase* pActionBase = dynamic_cast<GcmpActionBase*>(pAction);
        if (pActionBase)
        {
            pActionBase->AddDynamicGroup(pageName, TransferOwnership(ribbonGroups), GBMP_TR(L"创建洞口"));
        }
    }
}

void AddStructureDynamicPannelUtil::SetupStripFoundationCreationContext(IAction* pAction, bool bSupportLine, bool bSupportArc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pAction, L"pAction为空",L"GDMPLab",L"2024-03-30");

    std::vector<std::wstring> cmdStrings;
    std::vector<std::wstring> names;
    std::vector<std::wstring> icons;
    if (bSupportLine)
    {
        cmdStrings.push_back(ID_CMD_CREATE_STRAIGHT_STRUCTURE_STRIP_FOUNDATION); names.push_back(GBMP_TR(L"直线条形基础")); icons.push_back(L":/images/sketchline/直线16px.png");
    }

    const std::wstring& pageName(ID_PAGE_GBMP_MODIFY);
    std::wstring contextualPageCaption = GBMP_TR(L"创建");
    contextualPageCaption += GBMP_TR(L"条形基础");

    {
        std::vector<OwnerPtr<IRibbonGroup>> ribbonGroups;
        OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(L"gmCreateStripFoundationDynamicGroup", GBMP_TR(L"绘制"));
        {
            OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(pAction, cmdStrings, names, icons);
            opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
        }
        ribbonGroups.push_back(TransferOwnership(opDrawGroup));

        GcmpActionBase* pActionBase = dynamic_cast<GcmpActionBase*>(pAction);
        if (pActionBase)
        {
            pActionBase->AddDynamicGroup(pageName, TransferOwnership(ribbonGroups), contextualPageCaption);
        }
    }

    IMainWindow::GetMainWindow()->UpdateControlStatus();
}

void AddStructureDynamicPannelUtil::SetupBeamCreationContext(IAction* pAction, bool bSupportLine, bool bSupportArc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pAction, L"pAction为空",L"GDMPLab",L"2024-03-30");

    std::vector<std::wstring> cmdStrings;
    std::vector<std::wstring> names;
    std::vector<std::wstring> icons;
    if (bSupportLine)
    {
        cmdStrings.push_back(ID_CMD_CREATE_STRAIGHT_STRUCTURE_BEAM); names.push_back(GBMP_TR(L"直梁")); icons.push_back(L":/images/sketchline/直线16px.png");
        cmdStrings.push_back(ID_CMD_CREATE_RECT_STRUCTURE_BEAM); names.push_back(GBMP_TR(L"矩形梁")); icons.push_back(L":/images/sketchline/矩形16px.png");
    }
    if (bSupportArc)
    {
        cmdStrings.push_back(ID_CMD_CREATE_ARC_3PTS_STRUCTURE_BEAM); names.push_back(GBMP_TR(L"弧梁")); icons.push_back(L":/images/sketchline/三点圆弧16px.png");
    }
    cmdStrings.push_back(ID_CMD_CREATE_STRUCTURE_BEAM_BY_PICK); names.push_back(GBMP_TR(L"拾取线")); icons.push_back(L":/images/sketchline/选择线16px.png");

    const std::wstring& pageName(ID_PAGE_GBMP_MODIFY);
    std::wstring contextualPageCaption = GBMP_TR(L"创建");
    contextualPageCaption += GBMP_TR(L"梁");
    {
        std::vector<OwnerPtr<IRibbonGroup>> ribbonGroups;
        OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(L"gmCreateBeamDynamicGroup", GBMP_TR(L"绘制"));
        {
            OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(pAction, cmdStrings, names, icons);
            opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
        }
        ribbonGroups.push_back(TransferOwnership(opDrawGroup));

        GcmpActionBase* pActionBase = dynamic_cast<GcmpActionBase*>(pAction);
        if (pActionBase)
        {
            pActionBase->AddDynamicGroup(pageName, TransferOwnership(ribbonGroups), contextualPageCaption);
        }


    }
    //

    IMainWindow::GetMainWindow()->UpdateControlStatus();
}

void AddStructureDynamicPannelUtil::SetupVariableSectionBeamCreationContext(IAction* pAction, bool bSupportLine, bool bSupportArc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pAction, L"pAction为空",L"GDMPLab",L"2024-03-30");

    std::vector<std::wstring> cmdStrings;
    std::vector<std::wstring> names;
    std::vector<std::wstring> icons;
    if (bSupportLine)
    {
        cmdStrings.push_back(ID_CMD_CREATE_STRAIGHT_STRUCTURE_VARIABLE_SECTION_BEAM); names.push_back(GBMP_TR(L"直梁")); icons.push_back(L":/images/sketchline/直线16px.png");
        cmdStrings.push_back(ID_CMD_CREATE_RECT_STRUCTURE_VARIABLE_SECTION_BEAM); names.push_back(GBMP_TR(L"矩形梁")); icons.push_back(L":/images/sketchline/矩形16px.png");
    }
    if (bSupportArc)
    {
        //cmdStrings.push_back(ID_CMD_CREATE_ARC_3PTS_STRUCTURE_VARIABLE_SECTION_BEAM); names.push_back(GBMP_TR(L"弧梁")); icons.push_back(L":/images/sketchline/三点圆弧16px.png");
    }
    cmdStrings.push_back(ID_CMD_CREATE_STRUCTURE_VARIABLE_SECTION_BEAM_BY_PICK); names.push_back(GBMP_TR(L"拾取线")); icons.push_back(L":/images/sketchline/选择线16px.png");

    const std::wstring& pageName(ID_PAGE_GBMP_MODIFY);
    std::wstring contextualPageCaption = GBMP_TR(L"创建");
    contextualPageCaption += GBMP_TR(L"变截面梁");

    {
        std::vector<OwnerPtr<IRibbonGroup>> ribbonGroups;
        OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(L"gmCreateVariableSectionBeamDynamicGroup", GBMP_TR(L"绘制"));
        {
            OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(pAction, cmdStrings, names, icons);
            opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
        }
        ribbonGroups.push_back(TransferOwnership(opDrawGroup));

        GcmpActionBase* pActionBase = dynamic_cast<GcmpActionBase*>(pAction);
        if (pActionBase)
        {
            pActionBase->AddDynamicGroup(pageName, TransferOwnership(ribbonGroups), contextualPageCaption);
        }
    }

    IMainWindow::GetMainWindow()->UpdateControlStatus();
}

void AddStructureDynamicPannelUtil::SetupCustomizedProfileBeamCreationContext(IAction* pAction, bool bSupportLine, bool bSupportArc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pAction, L"pAction为空",L"GDMPLab",L"2024-03-30");

    std::vector<std::wstring> cmdStrings;
    std::vector<std::wstring> names;
    std::vector<std::wstring> icons;
    if (bSupportLine)
    {
        cmdStrings.push_back(ID_CMD_CREATE_STRAIGHT_STRUCTURE_CUSTOMIZED_PROFILE_BEAM); names.push_back(GBMP_TR(L"直梁")); icons.push_back(L":/images/sketchline/直线16px.png");
        //cmdStrings.push_back(ID_CMD_CREATE_RECT_STRUCTURE_BEAM); names.push_back(GBMP_TR(L"矩形梁")); icons.push_back(L":/images/sketchline/矩形16px.png");
    }
    if (bSupportArc)
    {
        cmdStrings.push_back(ID_CMD_CREATE_ARC_3PTS_STRUCTURE_CUSTOMIZED_PROFILE_BEAM); names.push_back(GBMP_TR(L"弧梁")); icons.push_back(L":/images/sketchline/三点圆弧16px.png");
    }
    //cmdStrings.push_back(ID_CMD_CREATE_STRUCTURE_BEAM_BY_PICK); names.push_back(GBMP_TR(L"拾取线")); icons.push_back(L":/images/sketchline/选择线16px.png");

    const std::wstring& pageName(ID_PAGE_GBMP_MODIFY);
    std::wstring contextualPageCaption = GBMP_TR(L"创建");
    contextualPageCaption += GBMP_TR(L"自定义截面梁");

    std::vector<OwnerPtr<IRibbonGroup>> ribbonGroups;
    OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(L"gmCreateCustomizedProfileBeamDynamicGroup", GBMP_TR(L"绘制"));
    {
        OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(pAction, cmdStrings, names, icons);
        opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
    }
    ribbonGroups.push_back(TransferOwnership(opDrawGroup));

    GcmpActionBase* pActionBase = dynamic_cast<GcmpActionBase*>(pAction);
    if (pActionBase)
    {
        pActionBase->AddDynamicGroup(pageName, TransferOwnership(ribbonGroups), contextualPageCaption);
    }


    IMainWindow::GetMainWindow()->UpdateControlStatus();
}

void AddStructureDynamicPannelUtil::SetupColumnCreationContext(IAction* pAction, const std::wstring& pageName)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pAction != nullptr, L"无效Action",L"GDMPLab",L"2024-03-30");

    {
        std::vector<OwnerPtr<IRibbonGroup>> ribbonGroups;
        OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(L"gmPlaceColumnGroup", GBMP_TR(L"放置"));
        {
            OwnerPtr<ICommandButtonDefinition> opBtnDef = ICommandButtonDefinition::Create(ID_CMD_DRAW_STRUCTURE_VERTICAL_COLUMN, GBMP_TR(L"垂直柱"), GBMP_TR(L"垂直柱"),
                ID_CMD_DRAW_STRUCTURE_VERTICAL_COLUMN, L":/images/verticalColumn.png", ButtonStyle::TextUnderIcon);
            opDrawGroup->AddButton(TransferOwnership(opBtnDef));
        }
        {
            OwnerPtr<ICommandButtonDefinition> opBtnDef = ICommandButtonDefinition::Create(ID_CMD_DRAW_STRUCTURE_SLANTED_COLUMN, GBMP_TR(L"斜柱"), GBMP_TR(L"斜柱"),
                ID_CMD_DRAW_STRUCTURE_SLANTED_COLUMN, L":/images/slantedColumn.png", ButtonStyle::TextUnderIcon);
            opDrawGroup->AddButton(TransferOwnership(opBtnDef));
        }
        ribbonGroups.push_back(TransferOwnership(opDrawGroup));

        GcmpActionBase* pActionBase = dynamic_cast<GcmpActionBase*>(pAction);
        if (pActionBase)
        {
            pActionBase->AddDynamicGroup(pageName, TransferOwnership(ribbonGroups), GBMP_TR(L"创建柱"));
        }
    }
}

void AddStructureDynamicPannelUtil::SetupGridNewCreationContext(gcmp::IAction* pAction, const std::wstring& pageName)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pAction != nullptr, L"无效Action",L"GDMPLab",L"2024-03-30");

    std::vector<std::wstring> cmdStrings;
    std::vector<std::wstring> names;
    std::vector<std::wstring> icons;
    cmdStrings.push_back(ID_CMD_DRAWING_LINE_GRIDNEW); names.push_back(GBMP_TR(L"直线")); icons.push_back(L":/images/sketchline/直线16px.png");
    cmdStrings.push_back(ID_CMD_DRAWING_ARC_GRIDNEW_BY3PTS); names.push_back(GBMP_TR(L"圆弧")); icons.push_back(L":/images/sketchline/三点圆弧16px.png");
    cmdStrings.push_back(ID_CMD_DRAWING_ARC_GRIDNEW_BYCENTER); names.push_back(GBMP_TR(L"圆心-起点-终点弧")); icons.push_back(L":/images/sketchline/圆心起点终点弧16px.png");
    cmdStrings.push_back(ID_CMD_CREATE_GRID_NEW_BY_PICK); names.push_back(GBMP_TR(L"拾取线")); icons.push_back(L":/images/sketchline/选择线16px.png");

    {
        std::vector<OwnerPtr<IRibbonGroup>> ribbonGroups;
        OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(L"gmDrawGridDynamicGroup", GBMP_TR(L"绘制"));
        {
            OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(pAction, cmdStrings, names, icons);
            opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
        }
        {
            OwnerPtr<ICommandButtonDefinition> opBtnDef = ICommandButtonDefinition::Create(ID_CMD_DRAWING_GRID_GROUP_NEW, GBMP_TR(L"批量创建"), GBMP_TR(L"批量创建"),
                ID_CMD_DRAWING_GRID_GROUP_NEW, L":/images/grid_group.png", ButtonStyle::TextUnderIcon);
            opDrawGroup->AddButton(TransferOwnership(opBtnDef));
        }
        ribbonGroups.push_back(TransferOwnership(opDrawGroup));

        GcmpActionBase* pActionBase = dynamic_cast<GcmpActionBase*>(pAction);
        if (pActionBase)
        {
            pActionBase->AddDynamicGroup(pageName, TransferOwnership(ribbonGroups), GBMP_TR(L"创建轴网"));
        }
    }
}
